home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / HyperCard Related / XCMDs & XFCNs / Byrne's XCMDs&XFCNs / Source / ChosenPrinter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-18  |  6.3 KB  |  260 lines  |  [TEXT/MPS ]

  1. /*
  2.     ChosenPrinter XFCN v1.1
  3.     
  4.     ©1991 Apple Computer, Inc.; by Mike Byrne
  5.     
  6.     Returns what type of printer is currently selected, and, if it can find it, what the name
  7.     of said printer is in item 2 of the return; i.e. it might return "LaserWriter,Godzilla" if
  8.     Godzilla is the currently selected LaserWriter.
  9.     
  10.     Form:
  11.     ChosenPrinter()
  12.     
  13.     # the MPW 3.2 build commands:
  14.     C -b ChosenPrinter.c -mbg off
  15.         Link -w -t STAK -c WILD -rt XFCN=615 ∂
  16.             -m ENTRYPOINT ∂
  17.             -sg ChosenPrinter ∂
  18.             ChosenPrinter.c.o ∂
  19.             "{Libraries}HyperXLib.o" ∂
  20.             "{Libraries}Runtime.o" ∂
  21.             "{Libraries}Interface.o" ∂
  22.             "{CLibraries}StdCLib.o" ∂
  23.             -o "teststack"
  24. */
  25.  
  26. #include <Types.h>
  27. #include <Resources.h>
  28. #include <string.h>
  29. #include <Memory.h>
  30. #include <Packages.h>
  31. #include <Errors.h>
  32. #include "HyperXCmd.h"
  33.  
  34. #define NULL     '\0'
  35. #define NIL     0L
  36. #define    TRUE    1
  37. #define FALSE    0
  38.  
  39. #define kNumParams 0
  40.  
  41.  
  42. /* prototypes */
  43. void ErrorBack(XCmdPtr paramPtr, char *message);
  44. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  45. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  46. short SystemFolderRefNum(void);
  47. char* GetFirst255(Ptr resPtr);
  48.  
  49.  
  50. pascal void EntryPoint(XCmdPtr paramPtr)
  51. {
  52.     /* variable declarations */
  53.     Boolean*    orgResLoadPtr;
  54.     Boolean        orgResLoad;
  55.     short        orgResFile;
  56.     char        printerName[260];
  57.     char        dummyString[260];
  58.     OSErr        theErr;
  59.     Handle        resHandle;
  60.     short        printerResFile;
  61.     short        sysFolder;
  62.     
  63.     
  64.     /* move high and lock the parameters. */
  65.     MoveLockParams(paramPtr, paramPtr->paramCount);
  66.  
  67.     /* check for copyright or syntax help request */
  68.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  69.         ErrorBack(paramPtr, "v1.1, ©1991 Apple Computer, Inc.; by Mike Byrne");
  70.         UnlockParams(paramPtr, paramPtr->paramCount);
  71.         return;
  72.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  73.         ErrorBack(paramPtr, "ChosenPrinter syntax is 'ChosenPrinter()'");
  74.         UnlockParams(paramPtr, paramPtr->paramCount);
  75.         return;
  76.     }
  77.  
  78.     /* not a copyright or help request.       */     
  79.     /* check for correct number of parameters */
  80.     if (paramPtr->paramCount != kNumParams) {
  81.         ErrorBack(paramPtr, "Error: ChosenPrinter syntax is 'ChosenPrinter()'");
  82.         UnlockParams(paramPtr, paramPtr->paramCount);
  83.         return;
  84.     }
  85.  
  86.     /* First, save the old state. */
  87.     orgResFile = CurResFile();
  88.     (long) orgResLoadPtr = 0xA5E;
  89.     if (*orgResLoadPtr) {
  90.         orgResLoad = TRUE;
  91.     } else {
  92.         orgResLoad = FALSE;
  93.     }
  94.  
  95.     /* set the system file for the string and get it. */
  96.     UseResFile(0);
  97.     SetResLoad(TRUE);
  98.     resHandle = Get1Resource('STR ',-8192);
  99.     theErr = ResError();
  100.     if ((theErr != noErr) || (resHandle == NIL)) {
  101.         UseResFile(orgResFile);
  102.         SetResLoad(orgResLoad);
  103.         if ((theErr == resNotFound) || (resHandle == NIL)) {
  104.             ErrorBack(paramPtr, "No printer is selected.");
  105.         } else {
  106.             ErrorBack(paramPtr, "Error: Could not get the 'STR ' resource.");
  107.         }
  108.         UnlockParams(paramPtr, kNumParams);
  109.         return;
  110.     }
  111.     
  112.     /* now we have the printer name */
  113.     strcpy(printerName, GetFirst255(*resHandle));
  114.  
  115.     /* try to get the ID number for the current system folder. */
  116.     if ( !(sysFolder = SystemFolderRefNum())) {
  117.         UseResFile(orgResFile);
  118.         SetResLoad(orgResLoad);
  119.         ErrorBack(paramPtr, printerName);
  120.         UnlockParams(paramPtr, kNumParams);
  121.         return;
  122.     }
  123.  
  124.     /* open the printer file.  If there are any errors, just return the printer
  125.         type and don't worry about it. */
  126.     c2pstr(printerName);
  127.     printerResFile = OpenRFPerm(printerName, SystemFolderRefNum(), fsRdPerm);
  128.     p2cstr(printerName);
  129.     theErr = ResError();
  130.     if (theErr != noErr) {
  131.         UseResFile(orgResFile);
  132.         SetResLoad(orgResLoad);
  133.         ErrorBack(paramPtr, printerName);
  134.         UnlockParams(paramPtr, kNumParams);
  135.         return;
  136.     }
  137.  
  138.     /* get the PAPA resource.  Again, if none, leave with the printer name... */
  139.     UseResFile(printerResFile);
  140.     resHandle = Get1Resource('PAPA',-8192);
  141.     theErr = ResError();
  142.     if (theErr != noErr) {
  143.         CloseResFile(printerResFile);
  144.         UseResFile(orgResFile);
  145.         SetResLoad(orgResLoad);
  146.         ErrorBack(paramPtr, printerName);
  147.         UnlockParams(paramPtr, kNumParams);
  148.         return;
  149.     }
  150.  
  151.     /* only cat to the printer type if we have something. */
  152.     strcpy( dummyString, GetFirst255(*resHandle) );
  153.     if (strlen(dummyString) > 0) {
  154.         strcat( printerName, ",");
  155.         strcat( printerName, dummyString );
  156.     }
  157.     
  158.     /* clean up and go home. */
  159.     CloseResFile(printerResFile);
  160.     UseResFile(orgResFile);
  161.     SetResLoad(orgResLoad);
  162.     ErrorBack(paramPtr,printerName);
  163.     UnlockParams(paramPtr, kNumParams);
  164.     return;
  165. }
  166.  
  167.  
  168. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  169.     GetFirst255 takes a handle pointing to some record beginning with a Str255.
  170.     It reads the length byte and BlockMoves to the c string.   */
  171.     
  172. char* GetFirst255(Ptr resPtr)
  173. {
  174.     Byte*    lengthPtr;
  175.     short    length;
  176.     char    retString[260];
  177.     
  178.     (Ptr) lengthPtr = resPtr;
  179.     length = (short) *lengthPtr;
  180.     lengthPtr++;
  181.     BlockMove( (Ptr) lengthPtr, (Ptr) retString, (Size) length);
  182.     retString[length] = NULL;
  183.     return(retString);
  184. }
  185.  
  186.  
  187. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  188.     SystemFolderRefNum gives the directory id of the system folder.    */
  189.     
  190. short SystemFolderRefNum(void)
  191. {
  192.     SysEnvRec        theWorld;
  193.     short            volID;
  194.     char            pathName[256];
  195.     char            cDummy[128];
  196.     Str255            pasDummy;
  197.     
  198.      /* set strings to zero */
  199.     pathName[0] = NULL;
  200.     cDummy[0] = NULL;
  201.     pasDummy[0] = NULL;
  202.  
  203.     /* get the ref number for the system folder */
  204.     if (SysEnvirons(1, &theWorld)) {
  205.         return(0);
  206.     } else {
  207.         volID = theWorld.sysVRefNum;
  208.     }
  209.     return(volID);
  210.  
  211. }
  212.  
  213.  
  214.  
  215.  
  216.     
  217. /* allocate and load up paramPtr->returnValue with a string 
  218.    -------------------------------------------------------- */
  219. void ErrorBack(XCmdPtr paramPtr, char *message)
  220. {
  221.     Handle  mesHnd;
  222.  
  223.     /*
  224.         Allocate space for an error message.
  225.         Copy the string into it.
  226.         Return the handle to HyperCard.
  227.     */
  228.     mesHnd = NewHandle((long)(strlen(message)+1));
  229.     if (mesHnd == nil) return;
  230.     strcpy((char *)*mesHnd,message);
  231.     paramPtr->returnValue = mesHnd;
  232. }
  233.  
  234.  
  235.  
  236. /*  move high and lock down all parameters  
  237.     ----------------------------------------------------------------------- */
  238. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  239. {
  240.     short i;
  241.     
  242.     for(i=0; i <= paramCount-1; i++)
  243.     {
  244.         MoveHHi(paramPtr->params[i]);
  245.         HLock(paramPtr->params[i]);
  246.     }
  247. }
  248.  
  249.  
  250.  
  251.  
  252. /* unlock all parameter handles in the XCmdBlock  
  253.    ---------------------------------------------  */
  254. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  255. {    short i;
  256.     
  257.     for(i=0; i <= paramCount-1; i++)
  258.         { HUnlock(paramPtr->params[i]);}
  259. }
  260.